package yang.itcode.service.impl;

import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yang.itcode.exception.BusinessException;
import yang.itcode.mapper.CustomerMapper;
import yang.itcode.model.pojo.Customer;
import yang.itcode.service.CustomerService;
import yang.itcode.utils.ThreadUtil;

import static yang.itcode.constant.ErrorConstant.*;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public boolean addCustomer(JSONObject obj) {
        String name = obj.getStr("name");
        String contact = obj.getStr("contact");
        String phone = obj.getStr("phone");
        String email = obj.getStr("email");
        String address = obj.getStr("address");
        Integer userId = ThreadUtil.getCurrentId();
        if(name == null) {
            throw new  BusinessException(CUSTOMER_PARAM_EXCEPTION_CODE, CUSTOMER_PARAM_EXCEPTION_MESSAGE);
        }
        Customer customer = new Customer();
        customer.setName(name);
        customer.setContact(contact);
        customer.setPhone(phone);
        customer.setEmail(email);
        customer.setAddress(address);
        customer.setCreateUserId(userId);
        customer.setUpdateUserId(userId);
        return customerMapper.insert(customer) == 1;
    }

    @Override
    public PageInfo<JSONObject> getCustomerList(Integer pageNum, Integer pageSize, JSONObject condition) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        condition.set("status", 1);
        return new PageInfo<>(customerMapper.selectCustomerView(condition));
    }

    @Override
    public JSONObject getCustomerDetail(Integer id) {
        Customer customer = customerMapper.selectById(id);
        if(customer == null || customer.getStatus() != 1) {
            throw new BusinessException(CUSTOMER_NOT_EXIST_EXCEPTION_CODE, CUSTOMER_NOT_EXIST_EXCEPTION_MESSAGE);
        }
        JSONObject obj = new JSONObject();
        obj.set("id", customer.getId());
        obj.set("name", customer.getName());
        obj.set("contact", customer.getContact());
        obj.set("phone", customer.getPhone());
        obj.set("email", customer.getEmail());
        obj.set("address", customer.getAddress());
        return obj;
    }

    @Override
    public boolean updateCustomer(JSONObject obj) {
        Integer id = obj.getInt("id");
        String name = obj.getStr("name");
        String contact = obj.getStr("contact");
        String phone = obj.getStr("phone");
        String email = obj.getStr("email");
        String address = obj.getStr("address");
        if(id == null || name == null) {
            throw new BusinessException(CUSTOMER_PARAM_EXCEPTION_CODE, CUSTOMER_PARAM_EXCEPTION_MESSAGE);
        }
        Integer userId = ThreadUtil.getCurrentId();
        Customer customer = new Customer();
        customer.setId(id);
        customer.setName(name);
        customer.setContact(contact);
        customer.setPhone(phone);
        customer.setEmail(email);
        customer.setAddress(address);
        customer.setUpdateUserId(userId);
        return customerMapper.update(customer) == 1;
    }

    @Override
    public boolean deleteCustomer(Integer id) {
        Integer userId = ThreadUtil.getCurrentId();
        Customer customer = new Customer();
        customer.setId(id);
        customer.setStatus(0);
        customer.setUpdateUserId(userId);
        return customerMapper.update(customer) == 1;
    }
}
